#include "params.h"
#include <map>
#include <string>
#include <sstream>
#include <cstdio>

using namespace std;

namespace params {

map<string, string> raw_data;
std::ofstream *err_log_file = 0;

void init(const char *filename)
{
	err_log_file = new ofstream("error.log");
	raw_data.clear();
	FILE *fin = fopen(filename, "r");
	if (!fin)
	{
		fprintf(stderr, "File %s not found, now using default parameters\n", filename);
		return;
	}

	char buffer[1024];
	memset(buffer, 0, sizeof(buffer));

	while (fgets(buffer, 1023, fin))
	{
		int n = strlen(buffer);
		for (int i = 0; i < n; ++i) if (buffer[i] == '\n' || buffer[i] == '#')
		{
			for (int j = i; j < n; ++j) buffer[j] = 0;
			n = i;
			break;
		}

		char *p = strstr(buffer, "=");
		if (!p) continue;
		*p = 0;

		raw_data[string(buffer)] = string(p + 1);
	}

	fclose(fin);
}

}